//
//  HMDistrictViewController.m
//  HMMeituanHD
//
//  Created by apple on 2017/5/17.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMDistrictViewController.h"
#import "HMChangeCityController.h"
#import "HMNavigationController.h"

static NSString *leftCell = @"leftCell";
static NSString *rightCell = @"rightCell";


@interface HMDistrictViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *topView;
//选中的一级菜单索引
@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation HMDistrictViewController

@synthesize popoverView = _popoverView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

}

//点击切换城市
- (IBAction)clickChangeCityBtn:(id)sender {
    //移除区域控制器
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //创建目标控制器
    HMChangeCityController *changeCityVc = [HMChangeCityController new];
    
    HMNavigationController *nav = [[HMNavigationController alloc] initWithRootViewController:changeCityVc];
    //设置modal展示样式
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    //设置modal的转场样式 水平翻转
    nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    //进行modal展示 只要在同一个响应者链条上都可以执行modal操作
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
}





#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView == _popoverView.leftTableView) {
        
        return self.distrcits.count;
        
    }else {
    
        return self.distrcits[self.selectedIndex].subdistricts.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    UITableViewCell *cell;
    
    if (tableView == _popoverView.leftTableView) { //一级菜单
        
        cell = [tableView dequeueReusableCellWithIdentifier:leftCell];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:leftCell];
            
            //设置cell的背景图片
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_leftpart"]];
            cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_left_selected"]];
        }
        //设置数据
        cell.textLabel.text = self.distrcits[indexPath.row].name;
        
        //设置详情箭头
        if (self.distrcits[indexPath.row].subdistricts.count) {
            
            //设置箭头
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        } else { //避免重用,箭头错误设置
            
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        
    }  else {
    
        cell = [tableView dequeueReusableCellWithIdentifier:rightCell];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rightCell];
            //设置cell的背景图片
            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_rightpart"]];
            cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_right_selected"]];
        }
        //设置数据
        cell.textLabel.text = self.distrcits[self.selectedIndex].subdistricts[indexPath.row];
    }
    
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    /**
     * 发送区域改变通知:
     1> 点击一级菜单发送通知  没有子区域   传递: 一级模型
     2> 点击二级菜单发送通知  传递:  一级模型和二级名称
     */
    
    if (tableView ==_popoverView.leftTableView) { //一级菜单
        
        
        //判断是否包含子区域
        if (self.distrcits[indexPath.row].subdistricts) { //包含二级菜单
            
            //记录索引
            self.selectedIndex = indexPath.row;
            //刷新二级菜单
            [_popoverView.rightTableView reloadData];
            
        }else { //没有二级菜单,直接发送通知
        
            [HMNoteCenter postNotificationName:HMDistrictDidChangeNote object:nil userInfo:@{HMDistrictDidChangeNoteModelKey: self.distrcits[indexPath.row]}];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        
    } else { //二级菜单
    
        [HMNoteCenter postNotificationName:HMDistrictDidChangeNote object:nil userInfo:@{HMDistrictDidChangeNoteModelKey: self.distrcits[self.selectedIndex], HMDistrictDidChangeNoteSubtitleKey: self.distrcits[self.selectedIndex].subdistricts[indexPath.row]}];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - 懒加载


- (HMPopoverView *)popoverView{
    
    if (_popoverView == nil) {
        //添加二级联动视图
        _popoverView = [HMPopoverView popoverView];
        
        [self.view addSubview:_popoverView];
        //设置位置
        _popoverView.y = self.topView.height;
        //设置数据源
        _popoverView.leftTableView.dataSource = self;
        _popoverView.leftTableView.delegate = self;
        _popoverView.rightTableView.dataSource = self;
        _popoverView.rightTableView.delegate = self;
    }
    return _popoverView;
}


@end
